# Maintainer: Drew Waranis <drew@waran.is>

_realname=jsoncpp
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.9.6
pkgrel=3
pkgdesc="A C++ library for interacting with JSON (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/open-source-parsers/jsoncpp"
msys2_references=(
  "cpe: cpe:2.3:a:jsoncpp_project:jsoncpp"
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/open-source-parsers/jsoncpp/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-static-lib-name.patch")
sha256sums=('f93b6dd7ce796b13d02c108bc9f79812245a82e577581c4c9aabe57075c90ea2'
            '692d7ae7f102644aaf7a41f1f9eafd7b3eca45febd6bde8675afdabca85adb4f')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg \
    0001-static-lib-name.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DJSONCPP_WITH_CMAKE_PACKAGE=ON \
    -DBUILD_OBJECT_LIBS=OFF \
    -DJSONCPP_WITH_TESTS=OFF \
    ${extra_config[@]} \
    -S ${_realname}-${pkgver} \
    -B build-${MSYSTEM}

  cmake --build build-${MSYSTEM}
}

package() {
  DESTDIR="${pkgdir}" cmake --install build-${MSYSTEM}

  install -Dm644 "${_realname}-${pkgver}/LICENSE" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
